/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.Reflection;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ELE.EnterpriseLibrary.Common.Configuration.Design
{
  /// <summary>
  /// A windows form for selecting a property from a defined type.
  /// Works in conjuction with <see cref="ObjectPropertySelectorEditor"/>.
  /// </summary>
  public partial class ObjectPropertySelectorUI : Form
  {
    private Type objectType;
    /// <summary>
    /// 
    /// </summary>
    public ObjectPropertySelectorUI()
    {
      InitializeComponent();
    }
    /// <summary>
    /// This constructor is used to set up the properties list properly with the given type.
    /// </summary>
    /// <param name="type"></param>
    public ObjectPropertySelectorUI(Type type)
      : this()
    {
      this.objectType = type;
      this.labelType.Visible = true;
      this.labelType.Text = this.objectType.FullName;
      this.listBoxProperties.BeginUpdate();
      if (type != null)
      {
        PropertyInfo[] properties = type.GetProperties();
        foreach(PropertyInfo property in properties)
        {
          if (property.CanRead && property.CanWrite)
          {
            this.listBoxProperties.Items.Add(property.Name);
          }
        }
      }
      this.listBoxProperties.EndUpdate();
    }

    private void buttonSelect_Click(object sender, EventArgs e)
    {
      DialogResult = DialogResult.OK;
      Close();
    }
    /// <summary>
    /// The selected property.
    /// </summary>
    public string SelectedProperty
    {
      get
      {
        return this.listBoxProperties.SelectedItem.ToString();
      }
    }
  }
}